package dji.ux.ext.setting.battery;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProvider;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

public class LowPowerPanel extends ConstraintLayoutWidget implements View.OnClickListener{
    private LowPowerModel lowPowerModel;
    private SchedulerProvider schedulerProvider;
    private SeekBar lowSeekbar,seriousLowSeekbar;
    private TextView tvLow,tvSerious;
    private int lastLowBattery;
    public LowPowerPanel(Context context){
        super(context);
    }

    public LowPowerPanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LowPowerPanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_low_power_warning, this);
        lowSeekbar = view.findViewById(R.id.seekbar_low);
        seriousLowSeekbar = view.findViewById(R.id.seekbar_serious);
        tvLow = view.findViewById(R.id.tv_low);
        tvSerious = view.findViewById(R.id.tv_serious);
        initSeekBar();
        setClickable(true);
        setFocusable(true);
        schedulerProvider = SchedulerProvider.getInstance();

        if (!isInEditMode()) {
            lowPowerModel = new LowPowerModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance(),
                    schedulerProvider);
        }


        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    private void initSeekBar(){
        lowSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvSerious.setText(progress + 15 + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                setLowBatterySeekbarValue(progress);
            }
        });
        seriousLowSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                setSeriousBattery(seekBar.getProgress() + 10);
            }
        });
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(lowPowerModel.getLowBattery()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateLowText));

        addReaction(lowPowerModel.getSeriousLowBattery()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateSeriousLowText));
    }

    private void updateLowText(int value){
        lowSeekbar.setProgress(value - 15);
        lastLowBattery = value;
        seriousLowSeekbar.setMax(value - 15);
        tvLow.setText(value + "%");
    }

    private void updateSeriousLowText(int value){
        tvSerious.setText(value + "%");
        seriousLowSeekbar.setProgress(value - 10);
    }

    //严重低电压
    private void setSeriousBattery(int value) {
        //The minimum value is 10. The maximum value is value from getLowBatteryWarningThreshold minus 5.
        addDisposable(lowPowerModel.setSeriousLowBattery(value).subscribe(() -> {
            tvSerious.setText(value + "%");
        }, logErrorConsumer(TAG, "setSeriousBattery: ")));
    }

    //低电压
    private void setLowBattery(int value) {
        //The percentage must be in the range of [15, 50]
        addDisposable(lowPowerModel.setLowBattery(value).subscribe(() -> {
            lastLowBattery = value;
        }, logErrorConsumer(TAG, "setLowBattery: ")));
    }

    private void setLowBatterySeekbarValue(int progress){
        if (progress >= 0 && progress <= lowSeekbar.getMax()) {
            lowSeekbar.setProgress(progress);
            tvLow.setText(progress + 15 + "%");
            if (progress < 5) {
                setLowBattery(progress + 15);
                seriousLowSeekbar.setMax(0);
            } else if (progress < seriousLowSeekbar.getProgress()) {
                //ToastUtil.show(R.string.low_battery_setup_failed);
                tvLow.setText(lastLowBattery + "%");
                lowSeekbar.setProgress(lastLowBattery - 15);
            } else {
                setLowBattery(progress + 15);
                seriousLowSeekbar.setMax(progress);
            }
        }
    }

    private void setSeriousBatterySeekbarValue(int progress){
        if (progress >= 0 && progress <= seriousLowSeekbar.getMax()) {
            seriousLowSeekbar.setProgress(progress);
            setSeriousBattery(progress + 10);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            lowPowerModel.setup();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            lowPowerModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }

    @Override
    public void onClick(View v) {

    }
}
